package com.fengniao.goods.service.impl;

import com.fengniao.goods.dao.CategoryMapper;
import com.fengniao.goods.dao.SpecMapper;
import com.fengniao.goods.pojo.Category;
import com.fengniao.goods.pojo.Spec;
import com.fengniao.goods.service.SpecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.DBOperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author lanhai
 * @date 2021/7/5 17:01
 */
@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper SpecMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 条件查询代码抽取
     * @param spec
     * @return
     */
    public Example getExample(Spec spec) {
        Example example = new Example(Spec.class);
        Example.Criteria criteria = example.createCriteria();

        if (spec != null) {
            if (!StringUtils.isEmpty(spec.getId())) {
                criteria.andEqualTo("id", spec.getId());
            }
            if (!StringUtils.isEmpty(spec.getName())) {
                criteria.andLike("name", "%" + spec.getName() + "%");
            }
            if (!StringUtils.isEmpty(spec.getOptions())) {
                criteria.andEqualTo("options", spec.getOptions());
            }
            if (!StringUtils.isEmpty(spec.getSeq())) {
                criteria.andEqualTo("seq", spec.getSeq());
            }
            if (!StringUtils.isEmpty(spec.getTemplateId())) {
                criteria.andEqualTo("template_id", spec.getTemplateId());
            }
        }
        return example;
    }

    @Override
    public PageInfo<Spec> findPage(Spec spec, Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Spec> Specs = findList(spec);
        return new PageInfo<>(Specs);
    }

    @Override
    public PageInfo<Spec> findPage(Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Spec> Specs = SpecMapper.selectAll();
        return new PageInfo<>(Specs);
    }

    @Override
    public List<Spec> findList(Spec spec) {

        Example example = getExample(spec);
        return SpecMapper.selectByExample(example);
    }

    @Override
    public List<Spec> findAll() {
        return SpecMapper.selectAll();
    }

    @Override
    public Spec findById(Integer id) {
        return SpecMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(Spec spec) {
        return SpecMapper.insertSelective(spec);
    }

    @Override
    public int revise(Spec spec) {
        return SpecMapper.updateByPrimaryKeySelective(spec);
    }

    @Override
    public int remove(Integer id) {
        return SpecMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Spec> findByCategoryId(Integer categoryId) {
        // 1.根据分类id查分类信息
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        // 2.再根据分类信息中的模板id查到规格信息
        Spec spec = new Spec();
        spec.setTemplateId(category.getTemplateId());
        List<Spec> specs = SpecMapper.select(spec);

        DBOperUtil.ListIsNull(specs);
        return specs;
    }
}
